
var settings = {
		url: contextPath+ '/omArea/query',
		dataType: 'json',
		colModel : [
			{display: '区域标识', name : 'areaId', width : 120, sortable : true, align: 'center'},
			{display: '区域名称', name : 'areaName', width : 120, sortable : true, align: 'center'},
			{display: '区域编码', name : 'areaCode', width : 120, sortable : true, align: 'center'},
			{display: '上级区域', name : 'parntAreaId', width : 120, sortable : true, align: 'center'},
			{display: '区域描述', name : 'areaDesc', width : 120, sortable : true, align: 'center'},
			{display: '创建时间', name : 'createDate', width : 180, sortable : true, align: 'center'}
			],
	    buttons : [
           {name : '新增', bclass : 'add', onpress : add},
           {separator : true}, 
           {name : '删除', bclass : 'delete', onpress : del},
           {separator : true}, 
           {name : '修改', bclass : 'edit', onpress : edit},
           {separator : true},
           {name : '导出', bclass : 'excel', onpress : excel},
           {separator : true}           
	       ],
		searchitems : [
			{display: '区域编码', name : 'areaCode'},
			{display: '区域名称', name : 'areaName', isdefault: true}
			],
		sortname: "areaId",
		sortorder: "asc",
		usepager: true,
		title: '区域列表',
		useRp: true,
		checkbox : true,// 是否要多选框
		rp: 10,
		showTableToggleBtn: true,
		width: 1000,
		height: 200,
		striped:false,
		onSuccess:addExcelHref
	};



$(function() {
	$("#grid").flexigrid(settings);   
});

function add(name,grid){
	var url = contextPath+'/omArea/add';
	var rs = openShowModal(url,1000,500);
	if(rs){
	   $('#grid').flexReload();//表格重载
	}
}

function del(name,grid){
	
	var  selected_count = $('.trSelected', grid).length;
    if (selected_count == 0) {
	    alert('请至少选择一条记录进行删除！');
	    return;
    }
    if (confirm("确定删除选中的记录?")) {
	  var ids = '';
	    $('tbody tr.trSelected', grid).each(function(index) {
	    	if(index)
	    		ids += ',';   		
	        ids += $('td:eq(0) div',this).text();
	    });
		
		var _url = contextPath+'/omArea/delete';
		$.ajax({
			url : _url,
			data : {'ids':ids},
			type : 'POST',
			dataType : 'json',
			success : function(result) {
				if (result.success == true) {
					alert('删除成功！');
					$('#grid').flexReload();//表格重载
				}
			}
		});
    }
  
	
}

function edit(name,grid){
	var  selected_count = $('.trSelected', grid).length;
    if (selected_count ==0) {
	    alert('请至少选择一条记录进行修改！');
	    return;
    }
    if (selected_count > 1) {
	    alert('只能选择一条记录进行修改！');
	    return;
    }
    var ids = '';
    $('tbody tr.trSelected', grid).each(function(index) {
    	if(index)
    		ids += ',';   		
        ids += $('td:eq(0) div',this).text();
    });
	var url = contextPath+'/omArea/edit/'+ids;
	var rs = openShowModal(url,1000,500);
	if(rs){
	   $('#grid').flexReload();//表格重载
	}
	
}

function excel(name, grid){
	
//	var _url = contextPath+'/omArea/excel';
	
//	window.open(_url);
//	$('#download').attr('href',_url);
//	$('#download').click();
 
}

function addExcelHref(){
	
	var settings = $.getQueryParam();
	
//	alert($.param(settings));
	
	var _url = contextPath+'/omArea/excel?'+$.param(settings);
	
	$('.excel').unbind("click").css('cursor','default').html('<a id="download" href="'+_url+'">导出</a>');
	
	$('.excel').click(function(e){
		e.stopPropagation();
		$('a',this).trigger("click");
		return false;
	});
}


/**
 * 打开模式窗口，窗口打开后居中。
 * url: 要打开的窗口链接
 * width: 窗口宽度
 * height: 窗口高度
 */
function openShowModal(url, width, height) {

	var ops = "dialogHeight:"+height+"px;dialogLeft:(screen.width/2-195/2)px;dialogTop:(screen.height/2-195/2)px;dialogWidth:"
		+width+"px;center:yes;resizable:yes;scroll:yes;status:yes;";

	return window.showModalDialog(url, '', ops);
}